package com.ksp.api.lucene;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ksp.dom.KSPDOM_ResDta;

import net.minidev.json.JSONArray;

@Controller
public class DataController {

	@Autowired
	IndexMapper indexMapper;

    public KSPDOM_ResDta createIndex(){
    	
    	Map<String, Object> wPamMap = new HashMap<>();
    	List<Map<String, Object>> qryTskInfLst = indexMapper.qryTskInf(wPamMap);
    	for (Map<String, Object> lineData : qryTskInfLst) {
    		  //创建Document对象
            Document doc = new Document();
            //获取每列数据
            Field wTchIdn = new Field("TCH_IDN",lineData.get("TCH_IDN").toString(),TextField.TYPE_STORED);
            Field wKspTtl = new Field("KSP_TTL",lineData.get("KSP_TTL").toString(),TextField.TYPE_STORED);
            Field wKspCon = new Field("KSP_CON",lineData.get("KSP_CON").toString(),TextField.TYPE_STORED);
            Field wImpDeg = new Field("IMP_DEG",lineData.get("IMP_DEG").toString(),TextField.TYPE_STORED);
            Field wDeaUsr = new Field("DEA_USR",lineData.get("DEA_USR").toString(),TextField.TYPE_STORED);
            Field wDeaUnm = new Field("DEA_UNM",lineData.get("DEA_UNM").toString(),TextField.TYPE_STORED);
            Field wTskSts = new Field("TSK_STS",lineData.get("TSK_STS").toString(),TextField.TYPE_STORED);
            Field wTskTyp = new Field("TSK_TYP",lineData.get("TSK_TYP").toString(),TextField.TYPE_STORED);
            Field wQuaUsr = new Field("QUA_USR",lineData.get("QUA_USR").toString(),TextField.TYPE_STORED);
            Field wQuaUnm = new Field("QUA_UNM",lineData.get("QUA_UNM").toString(),TextField.TYPE_STORED);
            
            //添加到Document中
            doc.add(wTchIdn);
            doc.add(wKspTtl);
            doc.add(wKspCon);
            doc.add(wImpDeg);
            doc.add(wDeaUsr);
            doc.add(wDeaUnm);
            doc.add(wTskSts);
            doc.add(wTskTyp);
            doc.add(wQuaUsr);
            doc.add(wQuaUnm);
            
            //调用，创建索引库
            IndexDemo.write(doc);
            //indexDemo.write(doc);
    	}
    	/*
        //查询数据库，必须要批量查询
        int fc = dao.foodcount();//查询总行数    8
        int start = 0;  //开始位置
        int rows = 5;   //每页行数
        
         * 0---4  5
         * 5---9  5
         * 10---14  5
         
        while(start<=rows){
            //每拉取一次数据
            List<Map<String, Object>> queryFood=dao.queryFood(start, rows);
            //获取字段
            for(int i=0;i<queryFood.size();i++){
                //获取每行数据
                Map<String, Object> lineData = queryFood.get(i);
              
            }
            start=rows+1;
        }*/
    	System.out.println("生成成功");
        return KSPDOM_ResDta.buildSucc("");
    }
    
    
    //搜索，实现高亮
    @RequestMapping("/com/KSPC01C")
    public KSPDOM_ResDta getFood(ServletRequest request) throws Exception{
    	String keyWord = request.getParameter("KSP_CON");
        List<Map> search = IndexDemo.search("KSP_CON", keyWord);
        return KSPDOM_ResDta.buildSucc(JSONArray.toJSONString(search));
    }
}
